package lab4_6;

import java.util.Scanner;

class Repo {

	String[] task;
	int flag = 0;
	int pos = 0;

	public Repo(String items) {
		task = items.split(" ");

	}

	public int getSize() {
		return task.length - pos;

	}

	public synchronized void run1() {

		while (getSize() > 0) {
			if (flag != 0) {
				try {
					wait();
				} catch (InterruptedException e) {

				}
			}
			System.out.println(Thread.currentThread().getName() + " finish " + task[pos]);
			pos++;
			flag = 1;
			notify();
		}
	}

	public synchronized void run2() {

		while (getSize() > 0) {
			if (flag != 1) {
				try {
					wait();
				} catch (InterruptedException e) {

				}
			}
			System.out.println(Thread.currentThread().getName() + " finish " + task[pos]);
			pos++;
			flag = 0;
			notify();
		}
	}
}

class Worker1 implements Runnable {
	private Repo repo;

	public Worker1(Repo repo) {
		this.repo = repo;
	}

	@Override
	public void run() {
		for (int i = 0; i < repo.getSize(); i++) {
			repo.run1();
		}
	}

}

class Worker2 implements Runnable {

	private Repo repo;

	public Worker2(Repo repo) {
		this.repo = repo;
	}

	@Override
	public void run() {
		for (int i = 0; i < repo.getSize(); i++) {
			repo.run2();
		}
	}
}

public class Main {

	public static void main(String[] args) throws InterruptedException {
		Scanner sc = new Scanner(System.in);
		Repo repo = new Repo(sc.nextLine());
		Thread t1 = new Thread(new Worker1(repo));
		Thread t2 = new Thread(new Worker2(repo));
		t1.start();
		Thread.yield();
		t2.start();
		sc.close();
	}

}
